<?php
class Behaviour {
	var $collection		= null;		
	var $cache			= null;
	var $clear			= null;	
	var $mongodb		= null;
	/*
	------- table behaviours
	------- field
	* userid: tv nhan
	* ownerid: tv tao
	* posterid: tv post
	* qcor: function get
	* qlike: function getUlike
	* qstotal: function getTotalCmt
	* qslist: function getCmt
	* qquote:
	* type: {
				1:blog
					11: hang xom viet blog moi
					12: co binh luan moi
					13: co thanh vien thich nhat ky cua toi
					14: co thanh vien thich binh luan nhat ky cua toi
				2:album
					21: hang xom dang anh moi
					22: co binh luan anh tai hinh anh toi tham gia
					23: co thanh vien thich hinh anh cua toi
					24: co thanh vien tag toi vao hinh					
				3:fullhouse
					31: hang xom chia se tam su moi
					32: co cmt moi tai nhung chi muc tam su toi co tham gia
					33: co thanh vien thich tam su cua toi
					34: co thanh vien thich binh luan tam su cua toi
				4:offline
					41: hang xom tao offline moi
					42: co cmt tai offline toi tham gia
				5:news
					51: hang xom dang tin
					52: co cmt moi tai ban tin toi tham gia
				6:place
					61: gioi thieu dia diem moi
					62: co cmt moi
					63: co hinh anh moi
			}
	* stype: {11,12,13,14,21,22,23,24,....}
	* shtopic: {1:show;0:hide;2:user denied topic}
	* shslist: {1:show;0:hide;2:user denied list cmt}
	* shlike: {1:show;0:hide;2:user denied list like}
	* shslike: {1:show;0:hide;2:user denied list like}
	* srcid
	* isread: {1:yes;0:no}
	* time
	*/
	public function __construct($dbName = DB_MONGO_VFRIEND) {
		if(DB_MONGO_ENABLE) {
			$this->mongodb 		= new Mongo();
			$this->collection 	= $this->mongodb->selectDB('vfriend')->selectCollection('behaviours');	
			$this->cache		= new Vfriend_Cache();
			$this->clear		= $this->cache->clear;
		}
	}
	public function deleteBySrcId($intType, $intSrcId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('type'=>$intType,'srcid'=>(int)$intSrcId));
		}
	}
	public function insert(	$intUserId, // thanh vien nhan topic
							$intOwnerId,  // thanh vien owner
							$intPosterId, // thanh vien post
							$strQueryCore, // get inform
							$strQueryLike, // get user like topic
							$strQueryTotalCmt, //get total cmt
							$strQueryCmt, // get list cmt top
							$strQueryQuote, // get quote
							$intType, // 
							$intSrcId) {	
		try {
			if(DB_MONGO_ENABLE) {					
				$objVfriendUser 	= new Vfriend_User();
				$arrUserDashboard	= $objVfriendUser->getUserNotification($intUserId);		
				$intShTopic			= 1;
				$intShCmt			= 1;
				$intShLike			= 1;
				$intShSlike			= 1;
				$intTypeIns			= 1;
				if($arrUserDashboard) {
					switch($intType) {
						case 11:
						case 12:
						case 13:
						case 14:
							$intTypeIns = 1;
							if(FUNCTION_NOTIFICAITON_DASHBOARD) {
								$intShTopic = (($arrUserDashboard['bdaskboard']&(1<<0)) > 0) ? 1 : 2;
								$intShCmt 	= (($arrUserDashboard['bdaskboard']&(1<<1)) > 0) ? 1 : 2;
								$intShLike 	= (($arrUserDashboard['bdaskboard']&(1<<2)) > 0) ? 1 : 2;
								$intShSlike = (($arrUserDashboard['bdaskboard']&(1<<3)) > 0) ? 1 : 2;
							}
							break;
						case 21:
						case 22:
						case 23:
						case 24:
							$intTypeIns 	= 2;
							if(FUNCTION_NOTIFICAITON_DASHBOARD) {
								$intShTopic 	= (($arrUserDashboard['idaskboard']&(1<<0)) > 0) ? 1 : 2;
								$intShCmt 		= (($arrUserDashboard['idaskboard']&(1<<1)) > 0) ? 1 : 2;
								$intShLike 		= (($arrUserDashboard['idaskboard']&(1<<2)) > 0) ? 1 : 2;
								$intShSlike 	= (($arrUserDashboard['idaskboard']&(1<<3)) > 0) ? 1 : 2;
							}
							break;
						case 31:
						case 32:
						case 33:
						case 34:
							$intTypeIns 	= 3;
							if(FUNCTION_NOTIFICAITON_DASHBOARD) {
								$intShTopic 	= (($arrUserDashboard['fdaskboard']&(1<<0)) > 0) ? 1 : 2;
								$intShCmt 		= (($arrUserDashboard['fdaskboard']&(1<<1)) > 0) ? 1 : 2;
								$intShLike 		= (($arrUserDashboard['fdaskboard']&(1<<2)) > 0) ? 1 : 2;
								$intShSlike 	= (($arrUserDashboard['fdaskboard']&(1<<3)) > 0) ? 1 : 2;
							}
							break;
						case 41:
						case 42:
							$intTypeIns 	= 4;
							if(FUNCTION_NOTIFICAITON_DASHBOARD) {
								$intShTopic 	= (($arrUserDashboard['odaskboard']&(1<<0)) > 0) ? 1 : 2;
								$intShCmt 		= (($arrUserDashboard['odaskboard']&(1<<1)) > 0) ? 1 : 2;
							}
							break;
						case 51:
						case 52:
						case 53:
						case 54:
							$intTypeIns 	= 5;
							break;
						case 61:
						case 62:
						case 63:
							$intTypeIns		= 6;
							break;
					}
				}
				if(!$this->checkRowExists($intUserId, $intSrcId, $intTypeIns, $intType)) {
					$objInsert = array(	'userid'	=> (int)$intUserId, 
										'ownerid'	=> (int)$intOwnerId,
										'posterid'	=> (int)$intPosterId,
										'qcor'		=> (string)$strQueryCore,
										'qlike'		=> (string)$strQueryLike,
										'qstotal'	=> (string)$strQueryTotalCmt,
										'qslist'	=> (string)$strQueryCmt,										
										'qquote'	=> (string)$strQueryQuote,
										'type' 		=> (int)$intTypeIns,
										'stype' 	=> (int)$intType,
										'shtopic' 	=> (int)$intShTopic,
										'shslist' 	=> (int)$intShCmt,
										'shlike' 	=> (int)$intShLike,
										'shslike' 	=> (int)$intShSlike,
										'srcid'		=> (int)$intSrcId,
										'time'		=> time(),
										'isread'	=> 0
								);		
					
					$this->collection->insert($objInsert);					
				} else {
					$this->update($intUserId, $intPosterId, $intSrcId, $intTypeIns, $intType, $strQueryQuote, $intShTopic, $intShCmt, $intShLike, $intShSlike);
				}
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Behaviour::insert - ' . $e->getMessage());			
			return null;
		}
	}
	public function update($intUserId, $intPosterId, $intSrcId, $intTypeIns, $intType, $strQueryQuote, $intShTopic, $intShCmt, $intShLike, $intShSlike) {
		try {
			if(DB_MONGO_ENABLE) {
				$objWhere 	= array('userid'	=> (int)$intUserId,
									'srcid'		=> (int)$intSrcId,
									'type'		=> (int)$intTypeIns
								);
				$objUpdate	= array('time'		=> time(),
									'posterid'	=> (int)$intPosterId,
									'stype'		=> (int)$intType,
									'qquote'	=> (string)$strQueryQuote,
									'shtopic'	=> (int)($intShTopic),
									'shslist'	=> (int)$intShCmt,
									'shlike'	=> (int)$intShLike,
									'shslike'	=> (int)$intShSlike,
									'isread'	=> 0
								);
				$objMulti	= array('multiple'=>true);
				$this->collection->update($objWhere, array('$set'=>$objUpdate),$objMulti);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Behaviour::update - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateRead($intUserId,$intSrcId,$intType,$intStype=0) {
		try {
			if(DB_MONGO_ENABLE) {
				if($intType == 2) {
					if($intStype > 21) {
						$objWhere 	= array('userid'	=> (int)$intUserId,
											'isread'	=> 0,										
											'stype'		=> array('$in'=>array(22,23,24)),
											'srcid'		=> (int)$intSrcId
										);
					} else {
						$objWhere 	= array('userid'	=> (int)$intUserId,
											'isread'	=> 0,										
											'stype'		=> 21,
											'srcid'		=> (int)$intSrcId
										);
					}
				} else {
					$objWhere 	= array('userid'	=> (int)$intUserId, 
										'isread'	=> 0,
										'srcid'		=> (int)$intSrcId);
				}
				$objUpdate	= array('isread'	=> 1);
				$objMulti	= array('multiple'	=> true);
				$this->collection->update($objWhere, array('$set'=>$objUpdate),$objMulti);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Behaviour::updateRead - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateReadById($intUserId, $strId) {
		try {
			if(DB_MONGO_ENABLE) {
				$objWhere 	= array('_id'		=> new MongoId($strId), 
									'userid'	=> (int)$intUserId);
				$objUpdate	= array('isread'	=> 1);
				$objMulti	= array('multiple'	=> true);
				$this->collection->update($objWhere, array('$set'=>$objUpdate),$objMulti);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Behaviour::updateReadById - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateShTopic($intUserId, $strId, $intShTopic=0) {
		try {
			if(DB_MONGO_ENABLE) {
				$objWhere 	= array('_id'		=> new MongoId($strId), 
									'userid'	=> (int)$intUserId,
									'shtopic'	=> array('$lt' => 2));
				$objUpdate	= array('shtopic'	=> (int)$intShTopic);
				$objMulti	= array('multiple'	=> true);
				$this->collection->update($objWhere, array('$set'=>$objUpdate),$objMulti);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Behaviour::updateShTopic - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateShTopicByReport($intType, $intStype, $intSrcId, $intShTopic=0) {
		try {
			if(DB_MONGO_ENABLE) {
				if($intType == 2) {
					if($intStype == 21) {
						$objWhere 	= array('type'		=> (int)$intType,
											'stype'		=> (int)$intStype,
											'srcid'		=> (int)$intSrcId,
											'shtopic'	=> array('$lt' => 2));
					} else {
						$objWhere	= array('type'		=> (int)$intType,
											'stype'		=> array('$in'=>array(22,23,24)),
											'srcid'		=> (int)$intSrcId,
											'shtopic'	=> array('$lt' => 2));
					}
				} else {
					$objWhere 	= array('type'		=> (int)$intType,
										'srcid'		=> (int)$intSrcId,
										'shtopic'	=> array('$lt' => 2));
				}
				$objUpdate	= array('shtopic'	=> (int)$intShTopic);
				$objMulti	= array('multiple'	=> true);
				$this->collection->update($objWhere, array('$set'=>$objUpdate),$objMulti);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Behaviour::updateShTopic - ' .$e->getMessage());			
			return null;
		}
	}
	public function checkRowExists($intUserId, $intSrcId, $intTypeIns, $intType=0) {
		try {
			if(DB_MONGO_ENABLE) {	
				if($intType > 21 && $intTypeIns == 2) {
					$objFind 	= array('userid'	=> (int)$intUserId,
										'srcid'		=> (int)$intSrcId,
										'type'		=> (int)$intTypeIns,
										'stype'		=> array('$in' => array(22,23,24))
									);
				} else {
					$objFind 	= array('userid'	=> (int)$intUserId,
										'srcid'		=> (int)$intSrcId,
										'type'		=> (int)$intTypeIns);
				}
				$intCount 	= $this->collection->find($objFind)->count();	
				return (isset($intCount) && $intCount > 0) ? 1 : 0 ;
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Behaviour::checkRowExists - ' .$e->getMessage());			
			return null;
		}
	}
	public function getBehaviourByUserId($intUserId, $intType, $intTime, $intOffset, $intLimit, &$intTotal=0) {
		try {						
			if(DB_MONGO_ENABLE) {							
				$objFind 	= array('userid'=>(int)$intUserId,'shtopic'=>1);
				if($intType > 0) {				
					$objFind['type'] = (int)$intType;
				}
				if($intTime > 0) {
					if($intTime == 1) {
						$intTime = strtotime(date('y-m-d 00:00:00'));
					} else {
						$intTime = strtotime("-$intTime day");
					}
					$objFind['time'] = array('$gt'=>(int)$intTime);
				}
				$objSort	= array('time'=>-1);
				$objCursor	= $this->collection->find($objFind);
				$arrData 	= $objCursor->sort($objSort)->limit($intLimit)->skip((int)$intOffset);
				$intTotal	= $objCursor->count();
				$arrResult 	= null;				
				if((object)$arrData) {	
					$objBlog	 	= new Blog();
					$objFullhouse	= new Fullhouse();
					$objPhoto		= new Photo();
					$objNews		= new News();
					$objOffline		= new Offline();
					$objPlace		= new Place();
					foreach($arrData as $value) {
						$intType			= $value['type'];
						$intStype			= $value['stype'];
						$intSrcId			= $value['srcid'];
						$value['postdate'] 	= date('Y-m-d H:i:s',$value['time']);						
						if($intType == 1) {
							$value['info']		= $value['qcor'] ? call_user_func(array($objBlog, $value['qcor']),$intSrcId) : null;
							$value['quote']		= $value['qquote'] ? call_user_func(array($objBlog, $value['qquote']),$intSrcId,$intStype) : null;
						}elseif($intType == 2) {
							if($intStype == 21) {
								$value['info']	= $value['qcor'] ? call_user_func(array($objPhoto, $value['qcor']),$intSrcId) : null;
							} else {
								$value['info']	= $value['qcor'] ? call_user_func(array($objPhoto, $value['qcor']),0,$intSrcId) : null;
								$value['quote']	= $value['qquote'] ? call_user_func(array($objPhoto, $value['qquote']),$intSrcId,$intStype) : null;
							}															
						}elseif($intType == 3) {
							$value['info']		= $value['qcor'] ? call_user_func(array($objFullhouse, $value['qcor']),$intSrcId) : null;
							$value['quote']		= $value['qquote'] ? call_user_func(array($objFullhouse, $value['qquote']), $intSrcId,$intStype) : null;
						}elseif($intType == 4) {
							$value['info']		= $value['qcor'] ? call_user_func(array($objOffline, $value['qcor']),$intSrcId) : null;
							$value['quote']		= $value['qquote'] ? call_user_func(array($objOffline, $value['qquote']), $intSrcId,$intStype) : null;
						}elseif($intType == 5) {
							$value['info']		= $value['qcor'] ? call_user_func(array($objNews, $value['qcor']),$intSrcId) : null;
							$value['quote']		= $value['qquote'] ? call_user_func(array($objNews, $value['qquote']), $intSrcId,$intStype) : null;
						}elseif($intType == 6) {
							$value['info']		= $value['qcor'] ? call_user_func(array($objPlace, $value['qcor']),$intSrcId) : null;
							$value['quote']		= $value['qquote'] ? call_user_func(array($objPlace, $value['qquote']), $intSrcId,$intStype) : null;
						}
						$arrResult[] 		= $value;
					}
				}
				return $arrResult;
			}
			return null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Behaviour::getBehaviourByUserId - ' .$e->getMessage());			
			return null;
		}
	}
	public function get($strId) {
		try {			
			if(DB_MONGO_ENABLE) {			
				$objFind 	= array('_id'=>new MongoId($strId));
				$arrData 	= $this->collection->find($objFind);	
				$arrResult 	= null;				
				if((object)$arrData) {	
					$objBlog 		= new Blog();
					$objFullhouse	= new Fullhouse();
					$objPhoto		= new Photo();
					$objNews		= new News();
					$objOffline		= new Offline();
					$objPlace		= new Place();
					foreach($arrData as $value) {
						$intType			= $value['type'];
						$intStype			= $value['stype'];
						$intSrcId			= $value['srcid'];
						$value['postdate'] 	= date('Y-m-d H:i:s',$value['time']);						
						if($intType == 1) {
							$value['info']		= $value['qcor'] ? call_user_func(array($objBlog, $value['qcor']),$intSrcId) : null;
							$value['slist']		= $value['qslist'] ? call_user_func(array($objBlog, $value['qslist']),$intSrcId) : null;
							$value['quote']		= $value['qquote'] ? call_user_func(array($objBlog, $value['qquote']),$intSrcId,$intStype) : null;
						}elseif($intType == 2) {
							if($intStype == 21) {
								$value['info']	= $value['qcor'] ? call_user_func(array($objPhoto, $value['qcor']),$intSrcId) : null;
							} else {
								$value['info']	= $value['qcor'] ? call_user_func(array($objPhoto, $value['qcor']),0,$intSrcId) : null;
								$value['slist']	= $value['qslist'] ? call_user_func(array($objPhoto, $value['qslist']),$intSrcId) : null;
								$value['quote']	= $value['qquote'] ? call_user_func(array($objPhoto, $value['qquote']),$intSrcId,$intStype) : null;
							}							
						}elseif($intType == 3) {
							$value['info']		= $value['qcor'] ? call_user_func(array($objFullhouse, $value['qcor']),$intSrcId) : null;
							$value['slist']		= $value['qslist'] ? call_user_func(array($objFullhouse, $value['qslist']),$intSrcId) : null;
							$value['quote']		= $value['qquote'] ? call_user_func(array($objFullhouse, $value['qquote']),$intSrcId,$intStype) : null;
						}elseif($intType == 4) {
							$value['info']		= $value['qcor'] ? call_user_func(array($objOffline, $value['qcor']),$intSrcId) : null;
							$value['slist']		= $value['qslist'] ? call_user_func(array($objOffline, $value['qslist']),$intSrcId) : null;
							$value['quote']		= $value['qquote'] ? call_user_func(array($objOffline, $value['qquote']), $intSrcId,$intStype) : null;
						}elseif($intType == 5) {
							$value['info']		= $value['qcor'] ? call_user_func(array($objNews, $value['qcor']),$intSrcId) : null;
							$value['slist']		= $value['qslist'] ? call_user_func(array($objNews, $value['qslist']),$intSrcId) : null;
							$value['quote']		= $value['qquote'] ? call_user_func(array($objNews, $value['qquote']), $intSrcId,$intStype) : null;
						}elseif($intType == 6) {
							$value['info']		= $value['qcor'] ? call_user_func(array($objPlace, $value['qcor']),$intSrcId) : null;
							$value['slist']		= $value['qslist'] ? call_user_func(array($objPlace, $value['qslist']),$intSrcId) : null;
							$value['quote']		= $value['qquote'] ? call_user_func(array($objPlace, $value['qquote']), $intSrcId,$intStype) : null;
						}
						$arrResult[] 		= $value;
					}
				}
				return isset($arrResult[0]) ? $arrResult[0] : null;
			}
			return null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Behaviour::get - ' .$e->getMessage());			
			return null;
		}
	}
	
}
?>